package cn.lili.common.utils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @Author xiangqian
 * @Date 2023/10/5 15:18
 */
public class DateTimeConversionExample {

    /**
     * 获取第二天时间
     *
     * @param timeString 时间 如:09:20
     */
    public static Date getTime(String timeString) {
        // 获取当前日期和时间
        Calendar calendar = Calendar.getInstance();

        // 将时间字符串解析为时间对象
        SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm");
        Date time = null;
        try {
            time = timeFormat.parse(timeString);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }

        // 设置时间部分
        Calendar nextDayCalendar = Calendar.getInstance();
        nextDayCalendar.set(Calendar.YEAR, calendar.get(Calendar.YEAR));
        nextDayCalendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH));
        nextDayCalendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH) + 1);
        nextDayCalendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY));
        nextDayCalendar.set(Calendar.MINUTE, calendar.get(Calendar.MINUTE));
        nextDayCalendar.set(Calendar.SECOND, calendar.get(Calendar.SECOND));

        // 添加年月日时分秒信息
        nextDayCalendar.set(Calendar.HOUR_OF_DAY, time.getHours());
        nextDayCalendar.set(Calendar.MINUTE, time.getMinutes());
        nextDayCalendar.set(Calendar.SECOND, time.getSeconds());

        // 转换为Date对象
        Date nextDayTime = nextDayCalendar.getTime();
        return nextDayTime;
    }
}
